package work.vcloud.baseinfo.controller;

import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

@RestController
@Scope("prototype")
public class AttachController extends BaseController {

	@Autowired
	OpenDao openDao;

	/**
	 * 添加分校
	 */
	@RequestMapping("api/unit.insertAttach")
	public OutputVo insertAttach() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("sqlMapId", "unit.selectAttachExit");
			Integer result = (Integer)openDao.queryForObject(map);
			if(result>0) {
				return output("9999","分校名称已存在","");
			}else {
				//插入单位
				map.put("type", "2");
				map.put("sqlMapId", "unit.insertUnit");
				map.put("id", openDao.insert(map));
				map.put("fk_unit",map.get("fk_center"));
	
				//插入分校
				map.put("sqlMapId", "unit.insertAttach");
				openDao.insert(map);
				return output("0000");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 修改分校
	 */
	@RequestMapping("api/unit.updateAttach")
	public OutputVo updateAttach() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("sqlMapId", "unit.selectAttachExit");
			Integer result = (Integer)openDao.queryForObject(map);
			if(result>0) {
				return output("9999","分校名称已存在","");
			}else {
				//修改单位
				map.put("sqlMapId", "unit.updateUnit");
				openDao.update(map);
	
				//修改分校
				map.put("sqlMapId", "unit.updateAttach");
				openDao.update(map);
				return output("0000");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 删除分校
	 */
	@RequestMapping("api/unit.deleteAttach")
	public OutputVo deleteAttach() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			//修改分校
			map.put("sqlMapId", "unit.deleteAttach");
			openDao.update(map);

			//修改单位
			map.put("sqlMapId", "unit.deleteUnit");
			openDao.update(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 清理分校
	 */
	@RequestMapping("api/unit.clearAttach")
	public OutputVo clearAttach() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			//修改分校
			map.put("sqlMapId", "unit.clearAttach");
			openDao.delete(map);

			//修改单位
			map.put("sqlMapId", "unit.clearUnit");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
}
